package com.cloudptt.api.comm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.cloudptt.api.product.log.Log;

/**
 * @author SunnyCoffee
 * @date 2014-1-27
 * @version 1.0
 * @desc desc 公共常量
 * 
 */
public class Common {

	private static final String THIS_FILE = "Common";
	public static final String LOCATION = "location";
	public static final String LOCATION_ACTION = "locationAction";

	public static String getDateAndTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format( new Date());
	}
	public static String getDateAndTime_s() {
		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		return format.format( new Date());
	}

	public static String getDate() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format( new Date());
	}

	public static String getRandomString() {
		StringBuffer strBuf = new StringBuffer("abc");
		for(int i = 0; i < 5; i++)
		{
			Random ran = new Random();
			int a = ran.nextInt(99999999);
			String str = String.valueOf(a);
			strBuf.append(str);
		}
		return strBuf.toString();
	}

	/**
	 * 发送消息时，获取当前事件
	 * 
	 * @return 当前时间
	 */
	public static String getFormatDate(String strDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long date = Long.valueOf(strDate)*1000;
		return format.format(new Date(date));
	}

	public static String getTime(int second) {
		if (second < 10) {
			return "00:0" + second;
		}
		if (second < 60) {
			return "00:" + second;
		}
		if (second < 3600) {
			int minute = second / 60;
			second = second - minute * 60;
			if (minute < 10) {
				if (second < 10) {
					return "0" + minute + ":0" + second;
				}
				return "0" + minute + ":" + second;
			}
			if (second < 10) {
				return minute + ":0" + second;
			}
			return minute + ":" + second;
		}
		int hour = second / 3600;
		int minute = (second - hour * 3600) / 60;
		second = second - hour * 3600 - minute * 60;
		if (hour < 10) {
			if (minute < 10) {
				if (second < 10) {
					return "0" + hour + ":0" + minute + ":0" + second;
				}
				return "0" + hour + ":0" + minute + ":" + second;
			}
			if (second < 10) {
				return "0" + hour + ":" + minute + ":0" + second;
			}
			return "0" + hour + ":" + minute + ":" + second;
		}
		if (minute < 10) {
			if (second < 10) {
				return hour + ":0" + minute + ":0" + second;
			}
			return hour + ":0" + minute + ":" + second;
		}
		if (second < 10) {
			return hour + ":" + minute + ":0" + second;
		}
		return hour + ":" + minute + ":" + second;
	}

	public static long getIntervalDate(String strDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		try {
			date = format.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return 0;
		}
		return date.getTime() / 1000;
	}

	public static String unicode2String(String unicodeStr) {
		StringBuffer sb = new StringBuffer();
		String tmp = null;
		while(unicodeStr.length()>=4)
		{

			tmp = unicodeStr.substring(0, 4) ;
			unicodeStr = unicodeStr.substring(4, unicodeStr.length());
			char c = (char) Integer.parseInt(tmp, 16);
			sb.append(c);
		}


		return sb.toString();
	}


	public static final String Key_Num_Begin = "sip:";
	public static final String Key_Num_IMPU = "@ims.com";
	public static final String Key_Num_IMPI = "@ims.mnc001.mcc001.3gppnetwork.org";

	public static final String Key_Num_McpttId = "@mcptt.com";
	public static final String Key_Num_McvideoId = "@mcvideo.com";
	public static final String Key_Num_McdataId = "@mcdata.com";


	public static String toSipNum(String num,String endStr)
	{
		Log.i(THIS_FILE, "toSipNum num = " +num + " endStr = " + endStr);
		StringBuffer strBuf = new StringBuffer(Key_Num_Begin);
		strBuf.append(num);
		strBuf.append(endStr);

		Log.i(THIS_FILE, "toSipNum = " +strBuf.toString() );

		return strBuf.toString();
	}

	public static String toImsSipNum(String num,String endStr)
	{
		Log.i(THIS_FILE, "toSipNum num = " +num + " endStr = " + endStr);
		StringBuffer strBuf = new StringBuffer(Key_Num_Begin);
		strBuf.append(num);
		strBuf.append(endStr);

		Log.i(THIS_FILE, "toSipNum = " +strBuf.toString() );

		return strBuf.toString();
	}

	public static String toSipUrl(String url)
	{
		Log.i(THIS_FILE, "toSipUrl url = " +url);
		StringBuffer strBuf = new StringBuffer(Key_Num_Begin);
		strBuf.append(url);

		Log.i(THIS_FILE, "toSipUrl = " +strBuf.toString() );

		return strBuf.toString();
	}

	/**
	 * 判断文件是否为图片<br>
	 * <br>
	 * @param pInput 文件名<br>
	 * @param pImgeFlag 判断具体文件类型<br>
	 * @return 检查后的结果<br>
	 * @throws Exception
	 */
	public static boolean isPicture(String pInput)
	{
		Log.i(THIS_FILE, "isPicture  pInput = " + pInput);
		// 文件名称为空的场合
		if(pInput != null
				&& pInput.length() <= 0){
			// 返回不和合法
			return false;
		}

		int index = pInput.lastIndexOf(".");
		if(index <= 0)
		{
			return false;
		}
		// 获得文件后缀名
		String tmpName = pInput.substring(index + 1,
				pInput.length());
		Log.i(THIS_FILE, "isPicture  tmpName = " + tmpName);
		// 声明图片后缀名数组
		String imgeArray [][] = {
				{"bmp", "0"}, {"dib", "1"}, {"gif", "2"},
				{"jfif", "3"}, {"jpe", "4"}, {"jpeg", "5"},
				{"jpg", "6"}, {"png", "7"} ,{"tif", "8"},
				{"tiff", "9"}, {"ico", "10"}
		};
		// 遍历名称数组
		for(int i = 0; i<imgeArray.length;i++){
			// 判断单个类型文件的场合
			if(imgeArray [i][0].equals(tmpName.toLowerCase())){
				return true;
			}
		}
		return false;
	}
	
	public static boolean isAudio(String pInput)
	{
		Log.i(THIS_FILE, "isPicture  pInput = " + pInput);
		// 文件名称为空的场合
		if(pInput != null
				&& pInput.length() <= 0){
			// 返回不和合法
			return false;
		}

		int index = pInput.lastIndexOf(".");
		if(index <= 0)
		{
			return false;
		}
		// 获得文件后缀名
		String tmpName = pInput.substring(index + 1,
				pInput.length());
		Log.i(THIS_FILE, "isAudio  tmpName = " + tmpName);
		// 声明图片后缀名数组
		String array [][] = {
				{"amr", "0"}, {"mp3", "1"}, {"wma", "2"}
		};
		// 遍历名称数组
		for(int i = 0; i<array.length;i++){
			// 判断单个类型文件的场合
			if(array [i][0].equals(tmpName.toLowerCase())){
				return true;
			}
		}
		return false;
	}
}
